package com.mukefile.action;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mukefile.pojo.ResponseData;
import com.mukefile.pojo.SysProfile;
import com.mukefile.service.SysProfileService;
import com.mukefile.utils.CommonUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/4
 */
@Controller("sysProfileAction")
@Scope("prototype")
public class SysProfileAction extends ActionSupport implements ModelDriven<SysProfile> {

    private final SysProfile sysProfile = new SysProfile();
    /**
     * 获取Request
     */
    private final Map<String, Object> request = (Map<String, Object>) ActionContext.getContext().get("request");
    /**
     * 获取Session
     */
    private final Map<String, Object> session = ActionContext.getContext().getSession();

    @Autowired
    SysProfileService sysProfileService;
    /**
     * 页码
     */
    private Integer page;
    /**
     * 每页数量
     */
    private Integer limit;
    private String idx;

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public String getIdx() {
        return idx;
    }

    public void setIdx(String idx) {
        this.idx = idx;
    }

    @Override
    public SysProfile getModel() {
        return this.sysProfile;
    }

    /**
     * 显示配置页面
     * @return 返回页面
     */
    public String showProfilePage(){
        return "sys_profile";
    }
    /**
     * 查询配置数据
     */
    public void queryProfile(){
        try {
            ResponseData<SysProfile> responseData = sysProfileService.querySysProfile(page, limit);
            CommonUtil.responseJSON(responseData);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public void addProfile(){

        try {
            // 执行插入
            int i = sysProfileService.addSysProfile(this.sysProfile);
            CommonUtil.responseJSON("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void editProfile(){
        try {
            // 执行编辑
            int i = sysProfileService.editSysProfile(this.sysProfile);
            CommonUtil.responseJSON("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void deleteProfile(){
        JSONArray jsonArray = JSONArray.parseArray(this.idx);
        List<SysProfile> list = new ArrayList<>(jsonArray.size());
        try {
            for (int i=0;i<jsonArray.size();i++){
                JSONObject jsonObject = jsonArray.getJSONObject(i);
                list.add(new SysProfile(
                        jsonObject.getIntValue("id"),
                        jsonObject.getString("profileName"),
                        jsonObject.getString("profileDescription"),
                        jsonObject.getString("profileValue")
                ));
            }
            // 执行删除
            this.sysProfileService.batchDeleteSysProfile(list);
            CommonUtil.responseJSON("ok");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void getJson()  {
        String kns = sysProfileService.getSysProfileValue("kns");
        System.out.println(kns);
    }
}
